<?php
class Merchant_Model_TakeStockItem
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_TakeStockItem();
	}
	
	
	
	public function getByCond($condition=array(),$type='*',$pageSize=0,$page=1,$orderby=array()){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        if(isset($condition['ts_id']) && $condition['ts_id']!=''){
            $select->where('ts_id=?', $condition['ts_id']);
        }
		if(isset($condition['ts_code']) && $condition['ts_code']!=''){
            $select->where('ts_code=?', $condition['ts_code']);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0){
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	/**
	 * 根据条件从take_stock_item表中读取一行
	 * @return 一维数组
	 */
	public function getRowByCond($condition=array()){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("1 =?",1);
        if(isset($condition['ws_code']) && $condition['ws_code']!=''){
            $select->where('ws_code=?', $condition['ws_code']);
        }
		if(isset($condition['ts_code']) && $condition['ts_code']!=''){
            $select->where('ts_code=?', $condition['ts_code']);
        }
		$sql=$select->__toString();
		return $this->_table->getAdapter()->fetchRow($sql);
	}
	
	public function getByCondGroup($condition=array(),$type='*',$pageSize=0,$page=1,$orderby=array()){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        if($type=='*'){
        	$select->from($table,$type);        	
        }else{
        	$select->from($table,'count(distinct ts_code) as total');
        }
        $select->where("1 =?",1);
        if(isset($condition['customer_id']) && $condition['customer_id']!=''){
            $select->where('customer_id=?', $condition['customer_id']);
        }
		if(isset($condition['ts_code']) && $condition['ts_code']!=''){
            $select->where('ts_code=?', $condition['ts_code']);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
        	$select->group(array('ts_code'));
            $select->order($orderby);
            if($pageSize>0 and $page>0){
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	
}